package com.itheima.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理器
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerException(SQLIntegrityConstraintViolationException ex){
        //获取错误信息
        String message = ex.getMessage(); //Duplicate entry 'zhangsan' for key 'idx_username'
        //解析出用户名
        String username = message.split(" ")[2];
        //响应错误提示信息
        return R.error("已存在，请不要重复添加");
    }

    @ExceptionHandler(CustomerException.class)
    public R<String> handlerException1(CustomerException ex){
        //获取错误信息
        String message = ex.getMessage();
        //响应错误提示信息
        return R.error(message);


    }

}
